package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @author Administrator
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private DishMapper dishMapper;

    //扩展方法:保存菜品,菜品口味数据
    @Override
    @Transactional
    public void saveDishWithFlavor(DishDto dishDto) {
        //1.添加菜品数据,往dish表中添加一条数据
        //只有保存完dish之后,id才会有值
        this.save(dishDto);

        //2.添加口味数据,往dish_flavor 表中添加多条数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        //TODO:核心步骤:给每个菜品口味设置id
        flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishDto.getId()));
        //批量保存
        dishFlavorService.saveBatch(flavors);

    }

    //扩展方法:分页查询菜品数据,包括分类名称
    @Override
    public Page<DishDto> pageDsihDto(Integer page, Integer pageSize, String name) {
        //创建分页对象
        Page<DishDto> pageInfo = new Page<>(page, pageSize);
        //调用mapper分页查询
        return dishMapper.pageDishDto(pageInfo, name);
    }

    //扩展方法:批量删除菜品,以及菜品口味数据,如果有菜品状态为启售,则不做操作
    @Override
    @Transactional
    public void deleteByIds(Long[] ids) {
        //1.根据菜品ids,查询其中是否有菜品为启售
        //select count(*) from dish where id in(ids) and status=1
        Integer count = this.lambdaQuery()
                .in(Dish::getId, ids)
                .eq(Dish::getStatus, 1)
                .count();
        if (count > 0) {
            throw new CustomerException("有菜品正在售卖,不能删除");
        }

        //2.删除菜品信息
        // delete from dish where id in (ids)
        this.removeByIds(Arrays.asList(ids));

        //3.删除菜品口味信息
        // delete from dish_flavor dish_id in (ids)
        dishFlavorService.lambdaUpdate()
                .in(DishFlavor::getId, ids)
                .remove();
    }

    //扩展方法:根据id查询菜品详细信息(包含多个口味数据)
    @Override
    public DishDto getDishByIdWithFlavor(Long id) {
        //SQL语句: select * from dish d left join dish_flavor df on d.id=df.dish_id where d.id=?
        DishDto dishDto = dishMapper.getDishByIdWithFlavor(id);
        return dishDto;
    }

    @Override
    @Transactional
    public void updateDishByIdWithFlavor(DishDto dishDto) {
        //1.修改菜品dish表数据
        this.updateById(dishDto);

        //2.修改菜品口味数据(先删在添加)
        //delete from dish_flavor where dish_id=?
        dishFlavorService.lambdaUpdate()
                .eq(DishFlavor::getDishId, dishDto.getId())
                .remove();
        List<DishFlavor> dishFlavorList = dishDto.getFlavors();
        //TODO:给每个口味设置dishId
        dishFlavorList.forEach(dishFlavor -> dishFlavor.setDishId(dishDto.getId()));
        //批量保存
        dishFlavorService.saveBatch(dishFlavorList);
    }

    //扩展方法:根据条件(菜品名称,分类id)查询菜品列表
    @Override
    public List<DishDto> listDishDto(Long categoryId, String name) {
        List<DishDto> dishDtoList=dishMapper.listDishDto(categoryId,name);
        return dishDtoList;
    }

    //扩展方法:根据id查询套餐对应的菜品信息
    @Override
    public List<DishDto> getDishBySetmealId(Long setmealId) {
        return dishMapper.getDishBySetmealId(setmealId);
    }


}


